﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlertSystem.Business;
using Utils;

namespace AlertSystem.Web
{
    public partial class SysGroupPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvSysGroup_LoadData();
            }
        }

        void grvSysGroup_LoadData()
        {
            grvSysGroup.DataSource = SysGroup.GetAll();
            grvSysGroup.DataBind(); // bind dữ liệu xuống client.
        }

        protected void lbtnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                Utils.ControlClass.ResetControlValues(dialogPanel);
                hdfObjectId.Value = "0";
                RegisterStartUpScript("OpenDialog('dialog-form',400);");
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void lbtRefresh_Click(object sender, EventArgs e)
        {
            grvSysGroup_LoadData();
        }

        protected void grvSysGroup_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName)
                {
                    case "Editing":
                        SysGroup obj = DB.SysGroups.FirstOrDefault(p => p.GroupID == id);
                        if (obj != null)
                        {
                            hdfObjectId.Value = obj.GroupID.ToString();
                            txtGroupName.Text = obj.GroupName;
                            txtDescription.Text = obj.Description;
                            txtOrd.Text = obj.Ord != null ? obj.Ord.ToString() : "";
                            //chkDisable.Checked = obj.Disabled;
                        }
                        RegisterStartUpScript("OpenDialog('dialog-form',400);");
                        break;
                    case "Deleting":
                        Authorize(Role.Write);
                        var deleteObj = DB.SysGroups.FirstOrDefault(p => p.GroupID == id);
                        if (deleteObj != null)
                        {
                            DB.SysGroups.DeleteOnSubmit(deleteObj);
                            DB.SubmitChanges();
                            InsertChangeLog("Xóa nhóm người dùng");
                            grvSysGroup_LoadData();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
        
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                int id = 0;
                int.TryParse(hdfObjectId.Value, out id);
                SysGroup obj;
                if (id == 0)//Thêm mới
                    obj = new SysGroup();
                else
                    obj = DB.SysGroups.FirstOrDefault(p => p.GroupID == id);
                obj.GroupCode = txtGroupName.Text.Trim().NameToTag();
                obj.GroupName = txtGroupName.Text.Trim();
                obj.Description = txtDescription.Text.Trim();
                int ord = 0;
                int.TryParse(txtOrd.Text.Trim(), out ord);
                obj.Ord = ord;
                obj.Disabled = false;

                if (ValidateInfo())
                {
                    if (id == 0)
                        DB.SysGroups.InsertOnSubmit(obj);
                    DB.SubmitChanges();
                    InsertChangeLog(id == 0 ? "Thêm mới nhóm người dùng" : "Cập nhật nhóm người dùng");
                    grvSysGroup_LoadData();
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected override bool ValidateInfo()
        {
            string errMsg = string.Empty;
            if (string.IsNullOrWhiteSpace(txtGroupName.Text))
            {
                errMsg += " - Tên.\\n";
            }
            if (errMsg.Length > 0)
            {
                errMsg += "Bắt buộc nhập\\n" + errMsg;
                RegisterStartUpScript(string.Format("OpenDialog('dialog-form',400); jAlert('{0}');", errMsg));
                return false;
            }
            return true;
        }
    }
}